package com.yylending.las.authority.register;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.common.RSAEntity;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by laixj on 2017/5/2.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class RegisterModel extends Model {
	private static final String TAG = "LoginModel";

	public final static int METHOD_REGISTER = 101;

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class, 120);
		switch (functionalCode) {
			case METHOD_REGISTER:
				return service.register("", new RSAEntity(params));
		}
		return null;
	}

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		BaseResultEntity result = cast(obj);
		switch (result.requestCode) {
			case METHOD_REGISTER:
				RegisterResult registerResult = cast(obj);
				EventAgent.post(registerResult);
				break;
		}
	}

	private interface Service {
		/**
		 * 注册账号
		 */
		@POST("server/cuser/register")
		Observable<RegisterResult> register(@Header("token") String token, @Body RSAEntity account);
	}

	static class RegisterResult extends BaseResultEntity<UserId> {
	}

    static class UserId{
        public String userId;
    }

	public static class RegisterAccount {
		/**
		 * 用户名，RSA加密
		 */
		public String mobile;
		/**
		 * 密码，RSA加密
		 */
		public String password;
		/**
		 * 验证码
		 */
		public String verify;
		/**
		 * 经度
		 */
		public String longitude;
		/**
		 * 纬度
		 */
		public String latitude;

		public RegisterAccount(String loginName, String password, String verifyCode, String longitude, String latitude) {
			this.mobile = loginName;
			this.password = password;
			this.verify = verifyCode;
			this.longitude = longitude;
			this.latitude = latitude;
		}
	}
}